import { defineStore } from 'pinia';
import pinia from '@/store';
import { userLogin } from '@/api/user';
// import router from '@/router';
export interface UserState {
    username: string;
    accessToken: string;
    refreshToken?: string;
    roles: Array<string>;
}
export const useUserStoreHook = defineStore('userInfo', {
    state: (): UserState => ({
        username: '大伟',
        accessToken: '',
        roles: ['common']
    }),
    getters: {},
    actions: {
        storeUserLogin(data) {
            return userLogin(data).then((res) => {
                this.username = res.data.username;
                this.roles = res.data.roles;
                this.accessToken = res.data.accessToken;
                return res;
            });
        }
        // stroeRefreshUserInfo() {
        //     if (this.username == '大伟' && this.accessToken != '') {
        //         refreshUserInfo({
        //             accessToken: this.accessToken
        //         })
        //             .then((res) => {
        //                 this.username = res.username;
        //                 this.roles = res.roles;
        //                 this.accessToken = res.accessToken;
        //             })
        //             .catch(() => {
        //                 this.accessToken = '';
        //             });
        //     }
        // }
    },
    // 所有数据持久化
    // persist: true,
    // 持久化存储插件其他配置
    persist: {
        // 修改存储中使用的键名称，默认为当前 Store的 id
        key: 'userInfo',
        // 修改为 sessionStorage，默认为 localStorage
        storage: window.sessionStorage,
        // 部分持久化状态的点符号路径数组，[]意味着没有状态被持久化(默认为undefined，持久化整个状态)
        // paths: ['obj.data', 'save.[].me'],
        paths: ['accessToken'],
        // serializer: {//序列化
        //   deserialize: parse,
        //   serialize: stringify
        // },
        beforeRestore: (ctx) => {
            console.log(`about to restore '${ctx.store.$id}'`)
        },
        afterRestore: (ctx) => {
            console.log(`just restored '${ctx.store.$id}'`)
        }

    }
});
export function useUserStore() {
    return useUserStoreHook(pinia);
}
